package com.fita.api.web.controller;

import java.io.File;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.opentravel.ota._2003._05.OTAVehLocSearchRS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.fita.api.dto.request.CancelExistingVehicleReservationRequest;
import com.fita.api.dto.request.LocationDetailRequest;
import com.fita.api.dto.request.LocationSearchRequest;
import com.fita.api.dto.request.VehicleReservationRequest;
import com.fita.api.dto.request.VehicleSearchRequest;
import com.fita.api.handler.WebserviceHandler;
import com.fita.entity.dto.ehi.response.LocationDetailResponse;
import com.fita.entity.dto.ehi.response.VehicleReservationCancleResponse;
import com.fita.entity.dto.ehi.response.VehicleReservationResponse;
import com.fita.entity.dto.ehi.response.VehicleSearchResponse;

/**
 *
 * @author Ark
 *
 */
@RestController
@RequestMapping("/api/vehicles")
public class VehicleApiController {
    
    private static final Logger logger = LoggerFactory.getLogger(VehicleApiController.class);
    
    @Autowired
    private WebserviceHandler webserviceHandler;

    /**
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "/search-available", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult searchAvailableVehicle(@RequestBody VehicleSearchRequest request,
                                             @RequestParam(name = "mock", defaultValue = "false") Boolean mock,Page page) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            if(mock) {
                
                File file = ResourceUtils.getFile("classpath:json/vehicle_available.json");
                VehicleSearchResponse response = JsonUtils.parse(file, VehicleSearchResponse.class);
                httpResult.success().data("response", response);
                return httpResult;
                
            }else {
                
                VehicleSearchResponse response = webserviceHandler.searchAvailableVehicle(request, page);
                httpResult.success().data("response", response);
            }
            
        }catch (BusinessException e) {
        	
            httpResult.fail(e.getCode()).message(e.getMessage());
            
            logger.error("#searchAvailableVehicle() - error is {}", e);
            
		} catch (Exception e) {
			
            httpResult.fail("500").message("请求失败");
            
            logger.error("#searchAvailableVehicle() - error is {}", e);
        }
        return httpResult;
    }
    
    /**
     * @param request
     *
     * @return
     */
    @RequestMapping(value = "/reservation-vehicle", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult reservationVehicle(@RequestBody VehicleReservationRequest request,@RequestParam("mock") Boolean mock) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            if(mock) {
            	
            	File file = ResourceUtils.getFile("classpath:json/reservison.json");
            	
                VehicleReservationResponse response = JsonUtils.parse(file, VehicleReservationResponse.class);
                
                httpResult.success().data("response", response);
            }
            else {
            	
            	VehicleReservationResponse response = webserviceHandler.reservationVehicle(request);
            	
            	// 下单后没有alamo订单号，下单失败。
            	if (StringUtils.isBlank(response.getConfID())) {
					
            		return httpResult.fail("500").message("alamo code is empty, vehicle reservation fail. ");
				}
                httpResult.success().data("response", response);
            }
        } catch (BusinessException e) {
        	
            httpResult.fail(e.getCode()).message(e.getMessage());
            
            logger.error("#searchAvailableVehicle() - error is {}", e);
            
        } catch (Exception e) {
        	
            httpResult.fail("500").message("请求失败");
            
            logger.error("#searchAvailableVehicle() - error is {}", e);
        }
        return httpResult;
    }
    
    /**
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "/search-location", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult locationSearchRequest(@RequestBody LocationSearchRequest request, @RequestParam("mock") Boolean mock, Page page) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            
            
            if(mock) {
                
                
                File file = ResourceUtils.getFile("classpath:json/location_search_alamo_national.json");
                
                Map<String, Object> map = JsonUtils.parse(file);

                httpResult.success().data("result", map);
                return httpResult;
                
//                if(company) {
//                    
//                    File file = ResourceUtils.getFile("classpath:json/location_search_alamo_national.json");
//                    
//                    Map<String, Object> map = JsonUtils.parse(file);
//
//                    httpResult.success().data("result", map);
//                    return httpResult;
//                    
//                }else{
//                    
//                    File file = ResourceUtils.getFile("classpath:json/location_search_enterprise.json");
//                    
//                    Map<String, Object> map = JsonUtils.parse(file);
//
//                    httpResult.success().data("result", map);
//                    return httpResult;
//                    
//                }
                

            } else {
                
                OTAVehLocSearchRS result = webserviceHandler.searchLocation(request, page);
                httpResult.success().data("result", result);

            }

        } catch (BusinessException e) {
        	
            httpResult.fail(e.getCode()).message(e.getMessage());
            
            logger.error("#searchAvailableVehicle() - error is {}", e);
            
        } catch (Exception e) {
        	
            httpResult.fail("500").message("请求失败");
            
            logger.error("#searchAvailableVehicle() - error is {}", e);
        }
        return httpResult;
        
    }
    
    
    
    /**
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "/location-detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult locationDetailRequest(@RequestBody LocationDetailRequest request,@RequestParam("mock") Boolean mock, Page page) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            if(mock) {
                
                File file = ResourceUtils.getFile("classpath:json/location_detail.json");
                
                LocationDetailResponse response = JsonUtils.parse(file, LocationDetailResponse.class);
                
                httpResult.success().data("response", response);
                
                return httpResult;
                
            }else {
                
                LocationDetailResponse response = webserviceHandler.locationDetail(request, page);
                
                httpResult.success().data("response", response);
            }
        } catch (BusinessException e) {
        	
            httpResult.fail(e.getCode()).message(e.getMessage());
            
            logger.error("#searchAvailableVehicle() - error is {}", e);
            
        } catch (Exception e) {
        	
            httpResult.fail("500").message("请求失败");
            
            logger.error("#searchAvailableVehicle() - error is {}", e);
        }
        return httpResult;
    }
    
    /**
     * @param request
     * 
     * @return
     */
    @RequestMapping(value = "/cancle-reservation", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult cancleReservationRequest(@RequestBody CancelExistingVehicleReservationRequest request,@RequestParam("mock") Boolean mock, Page page) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            if(mock) {
                
                File file = ResourceUtils.getFile("classpath:json/reservation_cancle.json");
                
                Map<String, Object> map = JsonUtils.parse(file);
                
                httpResult.success().data("result", map);
                
                return httpResult;
                
            }else {
                VehicleReservationCancleResponse response = webserviceHandler.cancelReservation(request);
                
                httpResult.success().data("response", response);
            }
        } catch (BusinessException e) {
        	
            httpResult.fail(e.getCode()).message(e.getMessage());
            
            logger.error("#searchAvailableVehicle() - error is {}", e);
            
        } catch (Exception e) {
        	
            httpResult.fail("500").message("请求失败");
            
            logger.error("#searchAvailableVehicle() - error is {}", e);
        }
        return httpResult;
    }
}
